package com.correction.servlet;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import com.correction.classes.Exercice;

public class ExecuteLangageJAVA extends IExecuteGenerique {
	String result = "";

	@Override
	public String getExecute() {
		return "java";
	}

	@Override
	public String getCommand() {
		return "javac";
	}

	@Override
	public void writeError(Process process2) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				process2.getErrorStream()));
		String line = "";
		try {
			while ((line = reader.readLine()) != null) {
				System.out.println("Error: " + line);
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String executeWithDirection(final String path, String fileName,
			String direction, String command, Exercice exercice) {
		// params
		String params = exercice.getParams();
		String[] paramsArray = params.split(";");

		System.out.println(command + " -d " + path + " " + path
				+ File.separator + fileName);
		Runtime runtime = Runtime.getRuntime();
		try {
			final Process process1 = runtime.exec(command + " -d " + path + " "
					+ path + File.separator + fileName);
			if (process1.waitFor() == 0) {
				String[] parts = fileName.split("\\.");
				final Process process2 = runtime.exec(getExecute() + " -cp "
						+ path + " " + direction + "." + parts[0]);
				System.out.println(getExecute() + " -cp " + path + " "
						+ direction + "." + parts[0]);
				// Consommation de la sortie standard de l'application externe
				// dans un Thread separe
				Thread th = new Thread() {
					public void run() {
						try {
							result = writeFileUpload(process2, path);
						} catch (IOException ioe) {
							// Consommation de la sortie d'erreur de
							// l'application externe dans un Thread separe
							writeError(process2);
						}
					}
				};
				th.start();
				th.join();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public String writeFileUpload(Process process2, String path)
			throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				process2.getInputStream()));
		String line = "";
		String textFile = "";
		while ((line = reader.readLine()) != null) {
			textFile = textFile + line;
		}
		FileWriter fw = new FileWriter(path + "\\File-Java.txt");
		BufferedWriter bw = new BufferedWriter(fw);
		bw.write(textFile);
		bw.close();
		reader.close();
		return textFile;
	}

	@Override
	public String executeWithoutDirection(final String path, String fileName,
			String command, Exercice exercice) {

		// params
		String params = exercice.getParams();
		String[] paramsArray = params.split(";");

		System.out.println(command + " -g " + path + File.separator + fileName);
		Runtime runtime = Runtime.getRuntime();
		try {
			final Process process1 = runtime.exec(command + " -g " + path
					+ File.separator + fileName);
			if (process1.waitFor() == 0) {
				String[] parts = fileName.split("\\.");
				String executeCommand = getExecute() + " -cp " + path + " "
						+ parts[0];
				for (int i = 0; i < paramsArray.length; i++) {
					executeCommand += " "+paramsArray[i];
				}

				final Process process2 = runtime.exec(executeCommand);
				System.out.println(executeCommand);
				// Consommation de la sortie standard de l'application externes
				// dans un Thread separe
				if (process2.waitFor() == 0) {
					Thread th = new Thread() {
						public void run() {
							try {
								result = writeFileUpload(process2, path);
							} catch (IOException ioe) {
								// Consommation de la sortie d'erreur de
								// l'application externe dans un Thread separe
								writeError(process2);
							}
						}
					};
					th.start();
					th.join();
				} else
					System.out.println("finding...");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}